import * as counterService from '../services/counter';
import key from 'keymaster';

export default {
    namespace: 'count',
    state: {
        record: 0,
        current: 0,
    },
    reducers: {
        update(state) {
            const newCurrent = state.current + 1;
            return { ...state,
                record: newCurrent > state.record ? newCurrent : state.record,
                current: newCurrent,
            };
        },
        minus(state) {
            return { ...state, current: state.current - 1};
        },
    },
    effects: {
        *add(action, { call, put }) {
            yield call(counterService.delay, 1000);
            yield put({ type: 'update' });
        },
    },
    subscriptions: {
        keyboardWatcher({ dispatch }) {
            key('⌘+up, ctrl+up', () => { dispatch({type:'add'}) });
        }
    }
};